define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wu/stockproductspec/index',
                    add_url: 'wu/stockproductspec/add',
                    edit_url: 'wu/stockproductspec/edit',
                    //del_url: 'wu/stockproductspec/del',
                    multi_url: 'wu/stockproductspec/multi',
                    table: 'wu_stockproductspec',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        //{checkbox: true},
                        //{field: 'id', title: __('Id')},
                        //{field: 'productspec_id', title: __('Productspec_id')},
                        {field: 'spectypename', title: __('產品類型'), operate:false, formatter: Table.api.formatter.normal},
                        {field: 'productspec.spectype_id', title: __('類型名稱'), visible: false, addClass: "selectpage", extend: "data-source='wu/spectype/index' data-field='name'"},
                        //
                        //{
                        //    field: 'productspec.spectype_id',
                        //    title: __('類型名稱'),
                        //    searchList: Config.spectypeList,
                        //    formatter: Table.api.formatter.normal
                        //},
                        {
                            field: 'productspec.name',
                            title: __('Productspec.name'),
                            operate: 'LIKE %...%',
                            placeholder: '模糊搜索，*表示任意字符'
                        },
                        {
                            field: 'productspec.specnumber',
                            title: __('Productspec.specnumber'),
                            operate: 'LIKE %...%',
                            placeholder: '模糊搜索，*表示任意字符'
                        },
                        {field: 'num', title: __('Num'), operate: false},
                        {field: 'minnum', title: __('Minnum'), operate: false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    title: __('生產記錄'),
                                    classname: 'btn btn-xs btn-warning btn-addtabs',
                                    icon: 'fa fa-dashboard',
                                    url: 'wu/stockproductspeclog/index'
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        },
                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'productspec.id', title: __('Productspec.id')},
                        //{field: 'productspec.spectype_id', title: __('Productspec.spectype_id')},
                        //{field: 'productspec.nbznumber', title: __('Productspec.nbznumber')},
                        //{field: 'productspec.lycl', title: __('Productspec.lycl')},
                        //{field: 'productspec.create_time', title: __('Productspec.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'productspec.update_time', title: __('Productspec.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                var myColumns = [];
                $.each(table.bootstrapTable('getHiddenColumns'), function (i, item) {
                    myColumns.push(item.field);
                });
                $.cookie('fa_wu_stockproductspec', JSON.stringify(myColumns), {expires: 365, path: '/'});
            });
            if ($.cookie('fa_wu_stockproductspec')) {
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_wu_stockproductspec')), function (i, item) {
                    table.bootstrapTable('hideColumn', item);
                });
            }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        kctx: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});